package com.gwz.cardserver.dto.fight;

import cn.hutool.core.util.RandomUtil;
import com.gwz.cardserver.consts.CardColorEnums;
import com.gwz.cardserver.consts.CardWeightEnums;
import lombok.Data;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 牌库
 */
@Data
public class LibraryModel {
    private LinkedList<CardDto> cardList;


    public LibraryModel() {
        create();
        shuffle();
    }

    public void init() {
        create();
        shuffle();
    }

    private void create() {
        cardList = new LinkedList<>();
        // 创建普通的牌
        for (int color = CardColorEnums.CLUB.getCode(); color <= CardColorEnums.SQUARE.getCode(); color++) {
            for (int weidht = CardWeightEnums.THREE.getCode(); weidht <= CardWeightEnums.TWO.getCode(); weidht++) {
                CardDto cardDto = CardDto
                        .builder()
                        .name(CardColorEnums.getByCode(color).name() + CardWeightEnums.getByCode(weidht).name())
                        .Color(color)
                        .weight(weidht)
                        .build();
                cardList.push(cardDto);
            }
        }

        // 大王 小羊
        CardDto sJoker = CardDto
                .builder()
                .name("SJoker")
                .Color(CardColorEnums.NONE.getCode())
                .weight(CardWeightEnums.SJOKER.getCode())
                .build();
        CardDto lJoker = CardDto
                .builder()
                .name("LJoker")
                .Color(CardColorEnums.NONE.getCode())
                .weight(CardWeightEnums.LJOKER.getCode())
                .build();
        cardList.push(sJoker);
        cardList.push(lJoker);
    }

    /**
     * 洗牌
     */
    private void shuffle() {
        List<CardDto> newList = new ArrayList<>();

        for (CardDto cardDto : cardList) {
            int index = RandomUtil.randomInt(0, newList.size() + 1);
            newList.add(index, cardDto);
        }
        cardList.clear();
        for (CardDto cardDto : newList) {
            cardList.push(cardDto);
        }
    }

    /**
     * 发牌
     *
     * @return
     */
    public CardDto deal() {
        return cardList.pop();
    }
}
